﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmCapNhatPhieuThuST
    Public Delegate Sub Fill()
    Public Shadows Refresh As Fill

    Private _LoaiPhieu As Integer
    Public Property LoaiPhieu() As Integer
        Get
            Return _LoaiPhieu
        End Get
        Set(ByVal value As Integer)
            _LoaiPhieu = value
        End Set
    End Property

    Private _ID As String
    Public Property ID() As String
        Get
            Return _ID
        End Get
        Set(ByVal value As String)
            _ID = value
        End Set
    End Property

    Private _LaInPhieu As Boolean
    Public Property LaInPhieu() As Boolean
        Get
            Return _LaInPhieu
        End Get
        Set(ByVal value As Boolean)
            _LaInPhieu = value
        End Set
    End Property

    Private _CTSieuThi As ST_PhieuThu
    Public Property PhieuThu() As ST_PhieuThu
        Get
            Return _CTSieuThi
        End Get
        Set(ByVal value As ST_PhieuThu)
            _CTSieuThi = value
        End Set
    End Property

    Private _Nam As Integer
    Public Property Nam() As Integer
        Get
            Return _Nam
        End Get
        Set(ByVal value As Integer)
            _Nam = value
        End Set
    End Property

    Private _Thang As Integer
    Public Property Thang() As Integer
        Get
            Return _Thang
        End Get
        Set(ByVal value As Integer)
            _Thang = value
        End Set
    End Property

    Private _ActionForm As String
    Public Property ActionForm() As String
        Get
            Return _ActionForm
        End Get
        Set(ByVal value As String)
            _ActionForm = value
        End Set
    End Property
    Public Function convert(ByVal s As String) As Integer
        Try
            Return Integer.Parse(s)
        Catch ex As Exception
            Return 0
        End Try
    End Function
    Public Function SinhSoChungTu() As String
        Try
            Dim MaDV As New System.Text.StringBuilder
            Dim lst As List(Of String) = (From ds In dbTV.ST_PhieuThus Where ds.InUsed _
                                          And ds.NgayCT.Year = Nam _
                                          And ds.LoaiPhieu = LoaiPhieu _
                                          Order By ds.SoPhieu Descending Select ds.SoPhieu).ToList()
            Dim CoMa As Boolean = False
            If lst.Count > 0 Then
                Dim iMaDV As Integer = 0
                iMaDV = (From ds In lst Select convert(ds)).Max
                MaDV.Append(iMaDV + 1)
                While MaDV.Length <= 5
                    MaDV.Insert(0, "0")
                End While
                CoMa = True
            Else
                MaDV.Append("000001")
                CoMa = True
            End If
            If Not CoMa Then
                MaDV.Append("000001")
            End If
            Return MaDV.ToString
        Catch ex As Exception
            Return ""
        End Try
    End Function
    Public Sub RefreshForm()
        txtSoCT.Text = SinhSoChungTu()
        txtNguoiNop.Text = ""
        txtDiaChi.Text = ""
        txtLiDoNop.Text = ""
        txtSoTien.EditValue = 0
        txtChungTuGoc.Value = 0
    End Sub
    Public Function CheckSave()
        Using db As New DataMainDataContext(ConnectionString)
            If String.IsNullOrEmpty(txtSoCT.Text) Then
                Return "Bạn chưa nhập số chứng từ"
            ElseIf dteNgayCT.Text = "" Then
                Return "Bạn chưa nhập số chứng từ"
            ElseIf dteNgayCT.DateTime.Value.Year <> Nam Or dteNgayCT.DateTime.Value.Month <> Thang Then
                Return "Ngày chứng từ không đúng tháng làm việc"
            ElseIf ActionForm = vAddAction Or ActionForm = vCopyAction Then
                Dim obj = (From ds In db.ST_PhieuThus Where ds.InUsed And ds.SoPhieu.Equals(txtSoCT.Text.Trim) _
                           And ds.NgayCT.Year = Nam _
                           Select ds).FirstOrDefault
                If obj IsNot Nothing Then
                    Return "Số chứng từ này đã tồn tại"
                End If
            ElseIf ActionForm = vEditAction Then
                If txtSoCT.Text <> PhieuThu.SoPhieu Then
                    Dim obj = (From ds In db.ST_PhieuThus Where ds.InUsed And ds.SoPhieu.Equals(txtSoCT.Text.Trim) _
                           And ds.NgayCT.Year = Nam _
                           Select ds).FirstOrDefault
                    If obj IsNot Nothing Then
                        Return "Số chứng từ này đã tồn tại"
                    End If
                End If
            End If
            Return ""
        End Using
    End Function
    Public Sub SetViewForm()
        txtSoCT.Properties.ReadOnly = True
        dteNgayCT.Properties.ReadOnly = True
        txtChungTuGoc.Properties.ReadOnly = True
        txtNguoiNop.Properties.ReadOnly = True
        txtLiDoNop.Properties.ReadOnly = True
        txtDiaChi.Properties.ReadOnly = True
        txtSoTien.Properties.ReadOnly = True
        txtMaDoiTuong.Properties.ReadOnly = True
        txtTenDoiTuong.Properties.ReadOnly = True
        btnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        btnGhiVaThem.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
    End Sub
    Public Sub save()
        Using db As New DataMainDataContext(ConnectionString)
            Select Case ActionForm
                Case vAddAction, vCopyAction
                    Dim obj As New ST_PhieuThu
                    With obj
                        .ID = CreateID()
                        .LoaiPhieu = LoaiPhieu
                        .ChungTuGoc = txtChungTuGoc.Value
                        .IDKhachHang_NCC = txtMaDoiTuong.Tag
                        .DiaChi = txtDiaChi.Text.Trim
                        .LyDoNop = txtLiDoNop.Text.Trim
                        .NgayCT = dteNgayCT.DateTime
                        .NguoiNop = txtNguoiNop.Text.Trim
                        .SoPhieu = txtSoCT.Text.Trim
                        .SoTien = CDec(txtSoTien.EditValue)

                        .EditedOn = dbTV.GetDateTimeServer
                        .InUsed = True
                        ID = .ID
                    End With
                    db.ST_PhieuThus.InsertOnSubmit(obj)
                Case vEditAction
                    Dim objE = (From ds In db.ST_PhieuThus Where ds.ID = PhieuThu.ID Select ds).FirstOrDefault
                    With objE
                        .LoaiPhieu = LoaiPhieu
                        .ChungTuGoc = txtChungTuGoc.Value
                        .DiaChi = txtDiaChi.Text.Trim
                        .LyDoNop = txtLiDoNop.Text.Trim
                        .NgayCT = dteNgayCT.DateTime
                        .NguoiNop = txtNguoiNop.Text.Trim
                        .SoPhieu = txtSoCT.Text.Trim
                        .SoTien = CDec(txtSoTien.EditValue)
                        .IDKhachHang_NCC = txtMaDoiTuong.Tag

                        .EditedOn = dbTV.GetDateTimeServer
                        .InUsed = True
                        ID = .ID
                    End With
            End Select
            db.SubmitChanges()
        End Using
    End Sub
    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        save()
        LaInPhieu = chkInPhieu.Checked
        Close()
    End Sub

    Private Sub btnGhiVaThem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaThem.ItemClick
        save()
        If Refresh IsNot Nothing Then
            Refresh()
        End If
        RefreshForm()
        LaInPhieu = chkInPhieu.Checked
    End Sub

    Private Sub btnDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDong.ItemClick
        Dim rs = MsgAskSaveData("Bạn có muốn lưu dữ liệu không?")
        Select Case rs
            Case vMsg.vReSult.Yes
                save()
                Close()
            Case vMsg.vReSult.Cancel
            Case vMsg.vReSult.No
                Close()
        End Select
    End Sub

    Private Sub btnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTroGiup.ItemClick

    End Sub

    Private Sub FrmCapNhatPhieuThuST_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Cập nhật phiếu thu siêu thị tháng " & Thang & " Năm " & Nam
        Select Case LoaiPhieu
            Case ELoaiChungTu.PhieuChi
                lblNguoiNhanNop.Text = "Người nhận"
                lblLyDoNhanNop.Text = "Lý do"
            Case ELoaiChungTu.PhieuThu
                lblNguoiNhanNop.Text = "Người nộp"
        End Select
        Select Case ActionForm
            Case vAddAction
                dteNgayCT.DateTime = DateSerial(Nam, Thang, Date.DaysInMonth(Nam, Thang))
                txtSoCT.Text = SinhSoChungTu()
                txtSoCT.Focus()
            Case vEditAction, vCopyAction, vViewAction
                If PhieuThu IsNot Nothing Then
                    If ActionForm = vEditAction Then
                        txtSoCT.Text = PhieuThu.SoPhieu
                    ElseIf ActionForm = vCopyAction Then
                        txtSoCT.Text = SinhSoChungTu()
                    ElseIf ActionForm = vViewAction Then
                        SetViewForm()
                        txtSoCT.Text = PhieuThu.SoPhieu
                    End If
                    txtMaDoiTuong.Tag = PhieuThu.IDKhachHang_NCC
                    txtMaDoiTuong.Text = (From ds In dbTV.DM_KhachHangs Where ds.ID = PhieuThu.IDKhachHang_NCC Select ds.MaKhachHang).FirstOrDefault
                    txtTenDoiTuong.Text = (From ds In dbTV.DM_KhachHangs Where ds.ID = PhieuThu.IDKhachHang_NCC Select ds.TenKhachHang).FirstOrDefault
                    dteNgayCT.DateTime = PhieuThu.NgayCT
                    txtNguoiNop.Text = PhieuThu.NguoiNop
                    txtDiaChi.Text = PhieuThu.DiaChi
                    txtLiDoNop.Text = PhieuThu.LyDoNop
                    txtSoTien.EditValue = PhieuThu.SoTien
                    txtChungTuGoc.Value = PhieuThu.ChungTuGoc
                End If
        End Select
    End Sub

    Private Sub FrmCapNhatPhieuThuST_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub FrmCapNhatPhieuThuST_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    End Sub

    Private Sub txtSoTien_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSoTien.EditValueChanged
        txtBangChu.Text = TienBangChu(CDec(txtSoTien.EditValue))
    End Sub

    Private Sub txtMaDoiTuong_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaDoiTuong.Leave
        Dim obj = (From ds In dbTV.DM_KhachHangs Where ds.InUsed And ds.MaKhachHang.Equals(txtMaDoiTuong.Text.Trim) Select ds).FirstOrDefault
        If obj IsNot Nothing Then
            txtMaDoiTuong.Text = obj.MaKhachHang
            txtTenDoiTuong.Text = obj.TenKhachHang
            txtMaDoiTuong.Tag = obj.ID
            txtDiaChi.Text = obj.DiaChi
        Else
            Dim frm As New DanhMuc.FrmDMKhachHang
            frm.LaChon = True
            frm.ShowDialog()
            If frm.objKH IsNot Nothing Then
                txtMaDoiTuong.Text = frm.objKH.MaKhachHang
                txtTenDoiTuong.Text = frm.objKH.TenKhachHang
                txtMaDoiTuong.Tag = frm.objKH.ID
                txtDiaChi.Text = frm.objKH.DiaChi
            Else
                txtDiaChi.Text = ""
                txtMaDoiTuong.Text = ""
                txtTenDoiTuong.Text = ""
                txtMaDoiTuong.Tag = Nothing
            End If
        End If
    End Sub
End Class